# @file    camera.launch.py
# @brief   driver usb camera and get image data
# @author  Mr.Wu
# @date    2025-06-06

# Copyright 2025 Mr.Wu

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import DeclareLaunchArgument,IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import LaunchConfiguration
from ament_index_python import get_package_share_directory
import os

def generate_launch_description():
    calibration_file_path=os.path.join(get_package_share_directory('camera_driver'),os.path.join('config','default_cam.yaml'))
    calib_file_path_arg=DeclareLaunchArgument("calibration_file_path",default_value=calibration_file_path,description="camera calibration file path")

    # 启动零拷贝环境配置node
    hobot_shm_node=IncludeLaunchDescription(
        PythonLaunchDescriptionSource(
            os.path.join(
                get_package_share_directory('hobot_shm'),
                'launch/hobot_shm.launch.py'))
    )
    
    # 启动相机驱动节点
    hobot_node=Node(
        package='hobot_usb_cam',
        executable='hobot_usb_cam',
        name='hobot_usb_cam',
        parameters=[
            {"camera_calibration_file_path": LaunchConfiguration("calibration_file_path")},
            {"frame_id": "default_cam"},
            {"framerate": 30}, # rate for publish image data 
            {"image_height": 480}, # image resolution
            {"image_width": 640},
            {"io_method": "mmap"}, # way to get image data from linux kernel to user space
            {"pixel_format": "yuyv"}, # select pixel format,lookup by command "v4l2-ctl --list-formats-ext",yuvy无需解码，通信快一些
            {"video_device": "/dev/video0"}, # change to your camera device,lookup by command "ls /dev/video*"
            #{"video_device": "/dev/video8"},
            {"zero_copy": False}],
            arguments=['--ros-args', '--log-level', 'warn'] # 不添加这个会有很多info日志输出
        ) # 

    ld=LaunchDescription()
    ld.add_action(hobot_shm_node)
    ld.add_action(calib_file_path_arg)
    ld.add_action(hobot_node)

    return ld